package com.uzai.console.service.user.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.User;
import com.uzai.common.entity.WechatUser;
import com.uzai.common.enums.IdentityEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.user.UserQuery;
import com.uzai.console.dto.user.usertype.*;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupIdDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupInfoDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.dto.user.wechatuser.WechatUserQuery;
import com.uzai.console.entity.NormalRewardRule;
import com.uzai.console.entity.UserType;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.mapper.NormalRewardRuleMapper;
import com.uzai.console.mapper.UserTypeGroupMapper;
import com.uzai.console.mapper.UserTypeMapper;
import com.uzai.console.mapper.WechatUserMapper;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.user.UserTypeService;
import com.uzai.console.vo.user.usertype.UserTypeInfoVo;
import com.uzai.console.vo.user.usertypegroup.UserTypeGroupInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class UserTypeServiceImpl implements UserTypeService {

    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private WechatUserMapper wechatUserMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private NormalRewardRuleMapper normalRewardRuleMapper;

    /**
     * 分页查询会员类型列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeQuery
     * @return null
     */
    public Page<UserTypeInfoVo> findUserTypeList(UserTypeQuery userTypeQuery){
        //新建返回到页面的LIST对象
        List<UserTypeInfoVo> userTypeInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<UserType> page = new Page<>();
        userTypeQuery.convert(page);
        List<UserType> userTypeList = userTypeMapper.findByList(page, userTypeQuery);

        if(userTypeList != null){
            for (UserType userType : userTypeList){
                UserTypeInfoVo userTypeInfoVo = new UserTypeInfoVo();
                BeanUtils.copyProperties(userType, userTypeInfoVo);
                //身份名称
                userTypeInfoVo.setIdentityName(IdentityEnum.getById(userType.getIdentity()).getDesc());
                //会员类型名称
                UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userType.getGroupId(), userType.getMerId());
                if(userTypeGroup != null){
                    userTypeInfoVo.setGroupName(userTypeGroup.getName());
                    userTypeInfoVo.setUpIdentity(userTypeGroup.getUpIdentity());
                }
                userTypeInfoVoList.add(userTypeInfoVo);
            }
        }

        //返回到页面分页对象
        Page<UserTypeInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(userTypeInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询会员类型详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeIdDto
     * @return null
     */
    public UserTypeInfoVo findUserTypeInfo(UserTypeIdDto userTypeIdDto){
        if(userTypeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        //查询页面用户
        UserType userType = userTypeMapper.selectById(userTypeIdDto.getId(), userTypeIdDto.getMerId());

        if(userType == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员类型不存在");
        }

        UserTypeInfoVo userTypeInfoVo =  new UserTypeInfoVo();

        BeanUtils.copyProperties(userType, userTypeInfoVo);

        //身份名称
        userTypeInfoVo.setIdentityName(IdentityEnum.getById(userTypeInfoVo.getIdentity()).getDesc());
        //会员类型名称
        UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userType.getGroupId(), userType.getMerId());
        if(userTypeGroup != null){
            userTypeInfoVo.setGroupName(userTypeGroup.getName());
            userTypeInfoVo.setUpIdentity(userTypeGroup.getUpIdentity());
        }

        return userTypeInfoVo;
    }

    /**
     * 修改跨身份升级
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeUpdateUpIdentityDto
     * @return null
     */
    public void updateUpIdentity(UserTypeUpdateUpIdentityDto userTypeUpdateUpIdentityDto){
        if(userTypeUpdateUpIdentityDto.getGroupId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "类型组id参数不能为空");
        }

        if(userTypeUpdateUpIdentityDto.getUpIdentity() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "是否支持跨身份升级参数不能为空");
        }

        //查询页面用户
        UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userTypeUpdateUpIdentityDto.getGroupId(), userTypeUpdateUpIdentityDto.getMerId());

        if(userTypeGroup == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员类型组不存在");
        }
        userTypeGroup.setUpIdentity(userTypeUpdateUpIdentityDto.getUpIdentity());
        userTypeGroup.setUpdateTime(DateUtil.getNowTime());
        userTypeGroupMapper.update(userTypeGroup);
    }

    /**
     * 修改是否默认
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeUpdateIsDefaultDto
     * @return null
     */
    public void updateIsDefault(UserTypeUpdateIsDefaultDto userTypeUpdateIsDefaultDto){
        if(userTypeUpdateIsDefaultDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id参数不能为空");
        }

        if(userTypeUpdateIsDefaultDto.getIsDefault() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "是否默认参数不能为空");
        }

        //不允许关闭，只能通过开启默认来达到其他关闭状态
        if(userTypeUpdateIsDefaultDto.getIsDefault().intValue() == 0 ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "至少需要设置一个默认类型！（请设置其他类型为默认类型，当前类型自动关闭。）");
        }

        //查询页面用户
        UserType userType = userTypeMapper.selectById(userTypeUpdateIsDefaultDto.getId(), userTypeUpdateIsDefaultDto.getMerId());

        if(userType == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员类型不存在");
        }

        Integer now = DateUtil.getNowTime();

        //开启时，需要同组同身份其他的全部关闭默认，只允许一个默认。因为只有一个开启，所以不允许关闭，如需关闭，必须通过开启别的来达到关闭状态
        if(userTypeUpdateIsDefaultDto.getIsDefault().intValue() == 1){
            //查询同组同身份的会员类型
            UserTypeQuery userTypeQuery = new UserTypeQuery();
            userTypeQuery.setMerId(userTypeUpdateIsDefaultDto.getMerId());
            userTypeQuery.setIdentity(userType.getIdentity());
            userTypeQuery.setGroupId(userType.getGroupId());
            List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
            if(userTypeList != null && userTypeList.size() > 0){
                for(UserType record : userTypeList){
                    //自己则开启默认
                    if(record.getId().longValue() == userType.getId().longValue()){
                        record.setIsDefault(1);
                    }else{//其他的会员类型全部关闭默认
                        record.setIsDefault(0);
                    }
                    record.setUpdateTime(now);
                    userTypeMapper.update(record);
                }
            }
        }
    }

    /**
     * 添加会员类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeInfoDto
     * @return null
     */
    @Transactional
    public void addUserType(UserTypeInfoDto userTypeInfoDto){
        if(userTypeInfoDto.getGroupId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员类型组");
        }

        if(StringUtils.isBlank(userTypeInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型名称");
        }

        if(userTypeInfoDto.getIdentity() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入选择会员身份");
        }

        if(userTypeInfoDto.getUpAuto() != null && userTypeInfoDto.getUpAuto() == 1){

//            //邀请总人数、订单类型、订单数、返利总金额至少填写一项
//            boolean flag = true;
//
//            if(userTypeInfoDto.getUpInviteCount() != null){
//                flag = false;
//            }
//
//            if(userTypeInfoDto.getUpOrderType() != null){ //订单类型不为空，则必须输入订单笔数
//                flag = false;
//                if(userTypeInfoDto.getUpOrderCount() == null){
//                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单笔数");
//                }
//                //throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择订单类型");
//            }
//
//            if(userTypeInfoDto.getUpTotalAmount() != null){
//                flag = false;
//            }
//
//            if(flag){
//                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数、订单类型、返利总金额至少填写一项");
//            }

            //邀请总人数、订单类型、订单数、返利总金额不能为空
            if(userTypeInfoDto.getUpInviteCount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数必须填写");
            }

            if(userTypeInfoDto.getUpInviteCount() < 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数不能小于0");
            }

            if(userTypeInfoDto.getUpOrderType() == null){ //订单类型不为空，则必须输入订单笔数
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择订单类型");
            }

            if(userTypeInfoDto.getUpOrderCount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "订单笔数必须填写");
            }

            if(userTypeInfoDto.getUpOrderCount() < 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "订单笔数不能小于0");
            }

            if(userTypeInfoDto.getUpTotalAmount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "返利总金额必须填写");
            }

            if(Tools.getNumberDecimal(userTypeInfoDto.getUpTotalAmount()) > 2){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "返利总金额小数位最多支持2位");
            }


//            if(StringUtils.isBlank(userTypeInfoDto.getUpRemsg())){
//                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入升级回复语");
//            }
        }

        UserType userType = new UserType();
        BeanUtils.copyProperties(userTypeInfoDto, userType);

        //是否默认（查询同组同身份的会员类型存不存在，存在就不默认，不存在就默认）
        UserTypeQuery userTypeQuery = new UserTypeQuery();
        userTypeQuery.setMerId(userTypeInfoDto.getMerId());
        userTypeQuery.setIdentity(userTypeInfoDto.getIdentity());
        userTypeQuery.setGroupId(userTypeInfoDto.getGroupId());
        List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
        if(userTypeList == null || userTypeList.size() == 0){
            userType.setIsDefault(1);
        }else{
            userType.setIsDefault(0);
        }
        Integer now = DateUtil.getNowTime();
        userType.setId(IdWorker.getId());
        userType.setCreateTime(now);
        userType.setUpdateTime(now);

        //排序,处理页面不填的情况，自动排名该组最后一名
        if(userType.getUpSort() == null){
            //查询数据中该组当中最大的排序后
            Integer maxUpSort = userTypeMapper.selectMaxUpSort(userType.getMerId(), userType.getGroupId());
            if(maxUpSort == null ){
                userType.setUpSort(1);
            }else{
                userType.setUpSort(maxUpSort + 1);
            }
        }

        userTypeMapper.insertSelective(userType);

        //修改分佣规则数据-增加默认配置
        //查询所有采用了该会员类型组的分佣规则
        NormalRewardRuleQuery normalRewardRuleQuery = new NormalRewardRuleQuery();
        normalRewardRuleQuery.setMerId(userTypeInfoDto.getMerId());
        normalRewardRuleQuery.setUserTypeGroupId(userTypeInfoDto.getGroupId());
        List<NormalRewardRule> normalRewardRuleList = normalRewardRuleMapper.findByList(normalRewardRuleQuery);
        if(normalRewardRuleList != null && normalRewardRuleList.size() > 0){
            for (NormalRewardRule normalRewardRule : normalRewardRuleList){
                if(normalRewardRule.getRewardLogic() != null){
                    JSONArray josnArray = JSONObject.parseArray(normalRewardRule.getRewardDetail());
                    if(josnArray == null ){
                        josnArray = new JSONArray();
                    }
                    String rewardDetail = "{\"userRatio\":0,\"parentRatio\":0,\"slParentRatio\":0,\"agentRatio\":0,\"slAgentRatio\":0,\"partnerRatio\":0,\"otherRatio\":0,\"apRewardBase\":\"0\",\"ladderRatioGroupId\":null,\"userType\":\"1513457341985579010\"}";
                    JSONObject jsonObject = JSONObject.parseObject(rewardDetail);
                    //替换会员类型id
                    jsonObject.put("userType", String.valueOf(userType.getId()));
                    josnArray.add(jsonObject);
                    normalRewardRule.setRewardDetail(JSONObject.toJSONString(josnArray));
                    normalRewardRuleMapper.updateSelective(normalRewardRule);
                }
            }
        }
    }

    /**
     * 修改会员类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeInfoDto
     * @return null
     */
    public void updateUserType(UserTypeInfoDto userTypeInfoDto){

        if(userTypeInfoDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员id");
        }

        //禁止手动修改会员类型组（会员类型组跟机器人配置绑定，禁止手动修改会员类型组）
//        if(userTypeInfoDto.getGroupId() == null ){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员类型组");
//        }

        if(StringUtils.isBlank(userTypeInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入类型名称");
        }

//        if(userTypeInfoDto.getIdentity() == null){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入选择会员身份");
//        }

        if(userTypeInfoDto.getUpAuto() != null && userTypeInfoDto.getUpAuto() == 1){

//            //邀请总人数、订单类型、返利总金额至少填写一项
//            boolean flag = true;
//
//            if(userTypeInfoDto.getUpInviteCount() != null){
//                flag = false;
//            }
//
//            if(userTypeInfoDto.getUpOrderType() != null){ //订单类型不为空，则必须输入订单笔数
//                flag = false;
//                if(userTypeInfoDto.getUpOrderCount() == null){
//                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单笔数");
//                }
//                //throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择订单类型");
//            }
//
//            if(userTypeInfoDto.getUpTotalAmount() != null){
//                flag = false;
//            }
//
//            if(flag){
//                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数、订单类型、返利总金额至少填写一项");
//            }


            //邀请总人数、订单类型、订单数、返利总金额不能为空
            if(userTypeInfoDto.getUpInviteCount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数必须填写");
            }

            if(userTypeInfoDto.getUpInviteCount() < 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "邀请总人数不能小于0");
            }

            if(userTypeInfoDto.getUpOrderType() == null){ //订单类型不为空，则必须输入订单笔数
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择订单类型");
            }

            if(userTypeInfoDto.getUpOrderCount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "订单笔数必须填写");
            }

            if(userTypeInfoDto.getUpOrderCount() < 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "订单笔数不能小于0");
            }

            if(userTypeInfoDto.getUpTotalAmount() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "返利总金额必须填写");
            }

            if(Tools.getNumberDecimal(userTypeInfoDto.getUpTotalAmount()) > 2){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "返利总金额小数位最多支持2位");
            }


//            if(StringUtils.isBlank(userTypeInfoDto.getUpRemsg())){
//                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入升级回复语");
//            }
        }

        UserType userType = userTypeMapper.selectById(userTypeInfoDto.getId(), userTypeInfoDto.getMerId());
        if(userType != null){
            //userType.setGroupId(userTypeInfoDto.getGroupId());
            userType.setName(userTypeInfoDto.getName());
            //userType.setIdentity(userTypeInfoDto.getIdentity());
            userType.setUpAuto(userTypeInfoDto.getUpAuto());
            userType.setUpInviteCount(userTypeInfoDto.getUpInviteCount());
            userType.setUpOrderType(userTypeInfoDto.getUpOrderType());
            userType.setUpOrderCount(userTypeInfoDto.getUpOrderCount());
            userType.setUpTotalAmount(userTypeInfoDto.getUpTotalAmount());
            userType.setUpReInte(userTypeInfoDto.getUpReInte());
            userType.setUpReBal(userTypeInfoDto.getUpReBal());
            userType.setUpRemsg(userTypeInfoDto.getUpRemsg());
            //单独处理排序字段
            if(userTypeInfoDto.getUpSort() != null){
                userType.setUpSort(userTypeInfoDto.getUpSort());
            }

            userTypeMapper.update(userType);
        }
    }

    /**
     * 删除会员类型
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeIdDto
     * @return null
     */
    public void deleteUserType(UserTypeIdDto userTypeIdDto){

        if(userTypeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员id");
        }

        UserType userType = userTypeMapper.selectById(userTypeIdDto.getId(), userTypeIdDto.getMerId());

        if(userType == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此会员类型不存在");
        }

        //是否默认，如果是默认类型，则需要判断是否是该分组内最后一个类型，不是最后一个类型，不让删除
        if(userType.getIsDefault() != null && userType.getIsDefault().intValue() == 1){
            //查询该分组内是否还有其他非默认类型
            UserTypeQuery userTypeQuery = new UserTypeQuery();
            userTypeQuery.setMerId(userType.getMerId());
            userTypeQuery.setIsDefault(0); //查询非默认
            userTypeQuery.setIdentity(userType.getIdentity());
            userTypeQuery.setGroupId(userType.getGroupId());
            List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
            if(userTypeList != null && userTypeList.size() > 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该类型组内还有非默认会员类型，请先删除非默认类型，最后才能删除默认类型");
            }
        }

        //判断此会员类型所在的组是否分佣规则在使用，如果有就不能删除
        NormalRewardRuleQuery normalRewardRuleQuery = new NormalRewardRuleQuery();
        normalRewardRuleQuery.setMerId(userType.getMerId());
        normalRewardRuleQuery.setUserTypeGroupId(userType.getGroupId());
        List<NormalRewardRule>  normalRewardRuleList = normalRewardRuleMapper.findByList(normalRewardRuleQuery);
        if(normalRewardRuleList != null && normalRewardRuleList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此会员类型组已经有分佣规则在使用，不能删除");
        }

        //查询此会员类型是否有用户在使用，如果有就不能删除
        WechatUserQuery userQuery = new WechatUserQuery();
        userQuery.setMerId(userTypeIdDto.getMerId());
        userQuery.setUserType(userTypeIdDto.getId());
        Page<WechatUser> page = new Page<>();
        page.setSize(5);
        page.setCurrent(1);
        List<WechatUser> wechatUserList = wechatUserMapper.findByList(page, userQuery);
        if(wechatUserList != null && wechatUserList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此会员类型已经有会员在使用，不能删除");
        }

        //删除
        userTypeMapper.deleteById(userTypeIdDto.getId(), userType.getMerId());
    }

    /**
     * 分页查询会员类型分组列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeGroupQuery
     * @return null
     */
    public Page<UserTypeGroupInfoVo> findUserTypeGroupList(UserTypeGroupQuery userTypeGroupQuery){
        //新建返回到页面的LIST对象
        List<UserTypeGroupInfoVo> userTypeGroupInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<UserTypeGroup> page = new Page<>();
        userTypeGroupQuery.convert(page);
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(page, userTypeGroupQuery);

        if(userTypeGroupList != null){
            for (UserTypeGroup userTypeGroup : userTypeGroupList){
                UserTypeGroupInfoVo userTypeGroupInfoVo = new UserTypeGroupInfoVo();
                BeanUtils.copyProperties(userTypeGroup, userTypeGroupInfoVo);
                userTypeGroupInfoVoList.add(userTypeGroupInfoVo);
            }
        }

        //返回到页面分页对象
        Page<UserTypeGroupInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(userTypeGroupInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加会员类型分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeGroupInfoDto
     * @return null
     */
    public void addUserTypeGroup(UserTypeGroupInfoDto userTypeGroupInfoDto){
        if(StringUtils.isBlank(userTypeGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        //验证是否重名
        UserTypeGroup userTypeGroup_name = userTypeGroupMapper.selectByName(userTypeGroupInfoDto.getMerId(), userTypeGroupInfoDto.getName());
        if(userTypeGroup_name != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
        }

        Integer now = DateUtil.getNowTime();
        UserTypeGroup userTypeGroup = new UserTypeGroup();
        userTypeGroup.setId(IdWorker.getId());
        userTypeGroup.setMerId(userTypeGroupInfoDto.getMerId());
        userTypeGroup.setName(userTypeGroupInfoDto.getName());
        userTypeGroup.setCreateTime(now);
        userTypeGroup.setUpdateTime(now);
        userTypeGroup.setUpIdentity(0); //默认不自动跨身份升级
        userTypeGroupMapper.insert(userTypeGroup);
    }

    /**
     * 删除会员类型分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeGroupIdDto
     * @return null
     */
    public void deleteUserTypeGroup(UserTypeGroupIdDto userTypeGroupIdDto){
        if(userTypeGroupIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择会员id");
        }

        //查询该分组是否已经被使用
        UserTypeQuery userTypeQuery = new UserTypeQuery();
        userTypeQuery.setMerId(userTypeGroupIdDto.getMerId());
        userTypeQuery.setGroupId(userTypeGroupIdDto.getId());
        List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
        if(userTypeList != null && userTypeList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员分组已经被使用，不能删除");
        }
        userTypeGroupMapper.deleteById(userTypeGroupIdDto.getId(), userTypeGroupIdDto.getMerId());
    }

    /**
     * 修改会员类型分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeGroupInfoDto
     * @return null
     */
    public void updateUserTypeGroup(UserTypeGroupInfoDto userTypeGroupInfoDto){

        if(userTypeGroupInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(StringUtils.isBlank(userTypeGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userTypeGroupInfoDto.getId(), userTypeGroupInfoDto.getMerId());
        if(userTypeGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该类型分组不存在");
        }

        //验证新名字是否已经存在
        if(!userTypeGroupInfoDto.getName().equals(userTypeGroup.getName())){ //表示修改了组名称
            //验证新名称是否存在
            UserTypeGroup userTypeGroup_name = userTypeGroupMapper.selectByName(userTypeGroupInfoDto.getMerId(), userTypeGroupInfoDto.getName());
            if(userTypeGroup_name != null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
            }
        }

        Integer now = DateUtil.getNowTime();
        userTypeGroup.setName(userTypeGroupInfoDto.getName());
        userTypeGroup.setUpdateTime(now);
        userTypeGroupMapper.update(userTypeGroup);
    }

    /**
     * 查询所有会员类型列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param userTypeQuery
     * @return null
     */
    public List<UserTypeInfoVo> findUserTypeListSelect(UserTypeQuery userTypeQuery){
        //新建返回到页面的LIST对象
        List<UserTypeInfoVo> userTypeInfoVoList = new ArrayList<>();
        List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);

        if(userTypeList != null){
            for (UserType userType : userTypeList){
                UserTypeInfoVo userTypeInfoVo = new UserTypeInfoVo();
                BeanUtils.copyProperties(userType, userTypeInfoVo);
                //身份名称
                userTypeInfoVo.setIdentityName(IdentityEnum.getById(userType.getIdentity()).getDesc());
                //会员类型组名称
                UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userType.getGroupId(), userType.getMerId());
                if(userTypeGroup != null){
                    userTypeInfoVo.setGroupName(userTypeGroup.getName());
                    userTypeInfoVo.setUpIdentity(userTypeGroup.getUpIdentity());
                }
                userTypeInfoVoList.add(userTypeInfoVo);
            }
        }

        //将分页信息复制到返回到页面的分页MO当中
        return userTypeInfoVoList;
    }


}
